<html>
<head><meta charset="utf-8"><title>Double symbol through FFI · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Double.20symbol.20through.20FFI.html">Double symbol through FFI</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="228757673"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Double%20symbol%20through%20FFI/near/228757673" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lurker <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Double.20symbol.20through.20FFI.html#228757673">(Mar 04 2021 at 11:47)</a>:</h4>
<p>Hi all - I've got a Rust application (app A) which is split in two sides, the Rust side and the C++ side. The Rust side on app A initializes a Rust library B, which is then used on the C++ side through FFI bindings offered by library B as well.</p>
<p>This works, but it's currently causing problems since app A ends up with two versions of the same global symbol from library B, and so initialization of library B needs to happen on both C++ and Rust sides of app A.</p>
<p>I'm wondering, what is usually the approach to deal with these sort of issues?</p>



<a name="228930068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Double%20symbol%20through%20FFI/near/228930068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lurker <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Double.20symbol.20through.20FFI.html#228930068">(Mar 05 2021 at 10:21)</a>:</h4>
<p>Or, I guess another way to ask this question is, is there a way to have a Rust library exposing a global lock through Rust's FFIs to C++, and using that to synchronize an application both from a Rust and C++ side?</p>



<a name="228931545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Double%20symbol%20through%20FFI/near/228931545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Double.20symbol.20through.20FFI.html#228931545">(Mar 05 2021 at 10:33)</a>:</h4>
<p>You could store a <code>pthread_mutex_t</code> in a <code>static</code> and export it to C++.</p>



<a name="228937259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Double%20symbol%20through%20FFI/near/228937259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lurker <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Double.20symbol.20through.20FFI.html#228937259">(Mar 05 2021 at 11:24)</a>:</h4>
<p>Thanks, but not sure I'm following. So using a pthread_mutex_t from both C++ and Rust sides?</p>



<a name="228939097"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Double%20symbol%20through%20FFI/near/228939097" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Double.20symbol.20through.20FFI.html#228939097">(Mar 05 2021 at 11:40)</a>:</h4>
<p>Yes</p>



<a name="229228766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Double%20symbol%20through%20FFI/near/229228766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lurker <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Double.20symbol.20through.20FFI.html#229228766">(Mar 08 2021 at 00:00)</a>:</h4>
<p>So, my problem, turns out, stems from linking my executable to two static libraries which define the same global symbol, but different name mangling.</p>



<a name="229228805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Double%20symbol%20through%20FFI/near/229228805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lurker <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Double.20symbol.20through.20FFI.html#229228805">(Mar 08 2021 at 00:00)</a>:</h4>
<p>Suppose I have the following:</p>
<ul>
<li>Application A written in both Rust and C++, which:<ul>
<li>links statically with a Rust library B (which contains the global symbol that's being duplicated);</li>
<li>links statically with a C++ library C, which is a C++ bindings wrapper around Rust libary B;</li>
<li>The Rust side of the Application A uses library B (the Rust crate), while the C++ side uses library C (the C++ bindings of the same library);</li>
</ul>
</li>
</ul>
<p>Initially Application A's Cargo.toml was pointing directly to the crate of library B. This was resulting in two definitions of the same global symbol, coming from library B and library C. However, by re-exporting the Rust symbols from library C, and using that crate instead in Application A's toml, this duplication no longer happens.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>